package com.example.supermarket.controller;//package com.example.supermarket;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.supermarket.Result;
import com.example.supermarket.entity.Manager;
import com.example.supermarket.service.ManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@RestController
public class LoginController {
    @Autowired
    ManagerService managerService;
    @PostMapping("/login")
    public Result<Manager> Login(@RequestBody Manager requestManager){
        String username = requestManager.getMgName();
        String password = requestManager.getMgPwd();
        QueryWrapper<Manager> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mg_name",username)
                    .eq("mg_pwd",password);
        QueryWrapper<Manager> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("mg_mobile",username)
                .eq("mg_pwd",password);
        Manager one = managerService.getOne(queryWrapper);
        Manager one1 = managerService.getOne(queryWrapper1);
        if (one != null) {
            return new Result<>(200,"ok",one);
        }else if(one1 != null) {
            return new Result<>(200,"ok",one1);
        }
        return new Result<>(0,"error",null);
//        if (!Objects.equals("admin", username) || !Objects.equals("123456", password)) {
//            String message = "账号密码错误";
//            return new Result<>(0,message,null);
//        } else {
//            return new Result<>(200,"success",null);
//        }
    }
//    @PostMapping("/login")
//    public Result<Manager> Login(@RequestParam("mgName") String mgName,
//                                 @RequestParam("mgPwd") String mgPwd){
//        if (!Objects.equals("admin", mgName) || !Objects.equals("123456", mgPwd)) {
//            return new Result<>(0,"error",null);
//        } else {
//            return new Result<>(200,"ok",null);
//        }
//    }
    @PostMapping("/registry")
    public Result<Manager> Registry(@RequestBody Manager manager){
//        Manager manager1 = new Manager();
//        manager1.setMgName(manager.getMgName());
//        manager1.setMgMobile(manager.getMgMobile());
//        manager1.setMgPwd(manager.getMgPwd());
        QueryWrapper<Manager> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mg_name", manager.getMgName());
        Manager one = managerService.getOne(queryWrapper);
        if (one != null) {
            return new Result<Manager>(0,"用户名已被占用",null);
        }
        boolean save = managerService.save(manager);
        if (save) {
            return new Result<Manager>(200, "ok", manager);
        }else{
            return new Result<Manager>(0,"error",null);
        }
    }
    @PostMapping("/findPwd")
    public Result<Manager> findPwd(@RequestBody Manager manager) {
        UpdateWrapper<Manager> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("mg_mobile", manager.getMgMobile())
                     .set("mg_pwd",manager.getMgPwd());
//        QueryWrapper<Manager> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("mg_mobile",manager.getMgMobile());
//        Manager one = managerService.getOne(queryWrapper);
//        managerService.get
        boolean update = managerService.update(updateWrapper);
        if (update) {
            return new Result<>(200,"ok",null);
        }
        return new Result<>(0,"error",null);



    }
}
